package com.wenwan.manage.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwan.manage.mapper.ReleaseTimesMapper;
import com.wenwan.manage.pojo.ReleaseTimes;
import com.wenwan.manage.service.ReleaseTimesService;
import com.wenwan.manage.setting.RegExpSetting;
import com.wenwan.manage.setting.StatusSetting;
import com.wenwan.manage.util.Util;

@Service
public class ReleaseTimesServiceImpl implements ReleaseTimesService {

	@Autowired
	private ReleaseTimesMapper releaseTimesMapper;

	@Override
	public Map<String, Object> create(String name, Double price, Double discount, Integer times, Integer giveTimes) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (name != null && name.length() < 1) {
			dto.put("data", false);
			dto.put("msg", "name" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("name" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (price != null && price < 0) {
			dto.put("data", false);
			dto.put("msg", "price" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ZERO);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("price" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ZERO);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (discount != null && discount < 0) {
			dto.put("data", false);
			dto.put("msg", "discount" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ZERO);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("discount" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ZERO);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (times != null && times < 0) {
			dto.put("data", false);
			dto.put("msg", "times" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ZERO);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("times" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ZERO);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (giveTimes != null && giveTimes < 0) {
			dto.put("data", false);
			dto.put("msg", "giveTimes" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ZERO);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("giveTimes" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ZERO);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", Util.UUID());
		map.put("createTime", Util.getTime());
		map.put("updateTime", Util.getTime());
		map.put("name", name);
		map.put("price", price);
		map.put("discount", discount);
		map.put("times", times);
		map.put("giveTimes", giveTimes);

		Boolean data = releaseTimesMapper.create(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> deleteById(String id) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);

		Boolean data = releaseTimesMapper.deleteById(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> update(String id, String name, Double price, Double discount, Integer times,
			Integer giveTimes) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (name != null && name.length() < 1) {
			dto.put("data", false);
			dto.put("msg", "name" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("name" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_ONE_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (price != null && price < 0) {
			dto.put("data", false);
			dto.put("msg", "price" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ZERO);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("price" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ZERO);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (discount != null && discount < 0) {
			dto.put("data", false);
			dto.put("msg", "discount" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ZERO);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("discount" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ZERO);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (times != null && times < 0) {
			dto.put("data", false);
			dto.put("msg", "times" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ZERO);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("times" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ZERO);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (giveTimes != null && giveTimes < 0) {
			dto.put("data", false);
			dto.put("msg", "giveTimes" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ZERO);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("giveTimes" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ZERO);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("name", name);
		map.put("price", price);
		map.put("discount", discount);
		map.put("times", times);
		map.put("giveTimes", giveTimes);
		map.put("updateTime", Util.getTime());

		Boolean data = releaseTimesMapper.update(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> selectAll() {
		// TODO Auto-generated method stub
		// DataToObject<List<ReleaseTimes>> dto = new
		// DataToObject<List<ReleaseTimes>>();
		Map<String, Object> dto = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();

		List<ReleaseTimes> data = releaseTimesMapper.selectAll(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> selectOneById(String id) {
		// TODO Auto-generated method stub
		// DataToObject<ReleaseTimes> dto = new DataToObject<ReleaseTimes>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);

		ReleaseTimes data = releaseTimesMapper.selectOneById(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

}
